<?php
namespace Home\Controller;
/**
 * 退货
 * User: linkwhy
 * Date: 2015/11/2
 * Time: 15:26
 */
class ReturnGoodsController extends HomeController{

    private $mode = '';
    private $mode_item = '';
    private $wechat_id = 0;

    public function _initialize(){
        $this->mode = M('return_goods');
        $this->mode_item = M('return_item');
        $this->wechat_id = $_SESSION['wechat_id'];
    }

    public function index(){
        $order = M('order');
        $order_id = I('get.order_id');
        $list = $order->alias('o')->field('c.id,c.goods_id,num,size,name,picture,pock_money as price')
                ->join('__ORDER_ITEM__ item ON item.order_id = o.order_id')
                ->join('__CART__ c ON item.cart_id = c.id')
                ->join('__GOODS__ g ON c.goods_id = g.id')
                ->where(array('o.order_id' => $order_id ))->select();

        $all_goods_prices = 0;
        foreach($list as $key => &$val){
            $val['allprice'] = floatval($val['price']) * intval($val['num']);
            $all_goods_prices += floatval($val['price']) * intval($val['num']);
        }
        $open_bank = M('clients','caitou_','DB_CONFIG1')->where(array('uid' => $_SESSION['userid']))->getField('bank_account');
        $data['all_goods_prices'] = $all_goods_prices;
        $data['order_id'] = $order_id;
        $data['name'] = "退货";
        $data['open_bank'] = $open_bank;
        $this->assign('data', $data);
        $this->assign('list', $list);
        $this->display();
    }

    public function check_user_bank(){
        $uid = $_SESSION['userid'];
        $count = M('clients','caitou_','DB_CONFIG1')->where( "uid = $uid and open_name != ''" )->count();
        if($count > 0){
            $this->ajaxReturn(array('status' => true));
        }else{
            $this->ajaxReturn(array('status' => false));
        }
    }

    /**
     * 申请退货
     */
    public function apply_return(){
        $order_id = I('post.order_id');
//        $cart_id = I('post.cart_id');
//        $row = $this->mode->field('id')->order('id desc')->limit(1)->find();
//        $rowcount1 = count($row) == 0 ? 0 : $row['id'];
//        $cdh = "RETURN".date('Ymd')."-".$this->wechat_id."-".($rowcount1+1);       //生成订单号
        $cdh = "RETURN" . build_order_no();
        $postData = array(
            'order_id' => $cdh,
            'order' => $order_id,
            'client_id' => $this->wechat_id,
            'return_money' => I('post.return_money'),
            'return_status' => 2,
            'return_time' => time(),
            'return_msg' => I('post.return_msg'),
        );
        if( $this->mode->add($postData) ){
            //修改退货状态
            M('order')->where(array('order_id' => $order_id))->setField('return_status', 2);
            $postlist = array();
            $card_id = I('post.cart_id');


            $where['client_id'] = $this->wechat_id;
            $where['order_id'] = $order_id;
            $where['is_create'] = 1;
            $where['item.cart_id'] = $card_id;
			
			$row = M('order_item')->alias('item')
				->field('goods_id,num')
				->join('__CART__ c ON item.cart_id = c.id')
				->where($where)->find();
			$postlist = array('order_id' => $cdh, 'goods_id' => $row['goods_id'], 'num' => $row['num']);
			if( $this->mode_item->add($postlist) ){
				$this->ajaxReturn(array('status' => true, 'info' => '退货成功！', 'url' => U('return_info', array('return_order_id' => $cdh))));
			}else{
				$this->ajaxReturn(array('status' => false, 'info' => '退货失败！'));
			}
        }
    }

    /**
     * 撤销退货
     */
    public function user_cancel(){
        $order = M('order');
        $rg = M('return_goods');
        //原订单号
        $order_id = I('post.order');
        $return_order = I('post.return_order');
        $der = A('Order');
        $result = $der->makeMoney($order_id, $_SESSION['shopid']);
        //$result = 1;
        if($result == 1){
            $rg->where(array('order_id' => $return_order))->setField('return_status', 10);
            $der->updateSaleCount($order_id);
            if($order->where(array('order_id' => $order_id))->save(array('is_clear' => 1, 'order_status' => 6, 'success_time' => time()))){
                $this->ajaxReturn(array('status' => true, 'info' => '撤销退货成功！', 'url' => U('Member/index')));
            }
        }
    }

    /**
     * 退单详细
     */
    public function return_info(){
        $rg = M('return_goods');
        $return_order_id = I('get.return_order_id');
        if( isset($return_order_id) ){
            $row = $rg->alias('rg')->field('rg.order_id,order,return_status,goods_id')->join('__RETURN_ITEM__ item ON rg.order_id = item.order_id', 'left')->where(array('rg.order_id' => $return_order_id))->find();
            $row2 = $this->company_address($row['goods_id']);
            $row2['return_order_id'] = $return_order_id;
            $row3 = M('clients','caitou_','DB_CONFIG1')->field('open_name,bank_account,open_bank_name')->where(array('uid' => $_SESSION['userid']))->find();
            $data = array_merge($row, $row2);
            $this->assign('data', $data);
            $this->assign('row', $row3);
            $this->display();
        }
    }

    public function company_address($goods_id){
        $ca = M('clients_address');
        $class_id = M('goods')->where(array('id' => $goods_id))->getField('class_id');
        return $ca->field('name,address,code,phone')->where(array('type' => 1, 'class_id'=> $class_id ))->find();
    }

    /**
     * 退单物流
     */
    public function return_wuliu(){
        $return_order_id = I('get.return_order_id');
        $data = M('return_goods')->field('wl_order,wl_name,user_msg')->where(array('order_id' => $return_order_id))->find();
        $data['return_order_id'] = $return_order_id;
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 添加退单物流信息
     */
    public function updateReturn(){
        $return_order_id = I('post.return_order_id');
        $postData = array(
            'wl_name' => I('post.wl_name'),
            'wl_order' => I('post.wl_order'),
            'user_msg' => I('post.user_msg'),
            'return_status' => 5
        );
        if( M('return_goods')->where(array('order_id' => $return_order_id))->save($postData) ){
            $this->ajaxReturn(array('status' => true, 'info' => '添加成功！', 'url' => U('return_info', array('return_order_id' => $return_order_id))));
        }
    }

    /**
     * 确认审核
     */
    public function sureVerify(){
        $rg = M('return_goods');
        $return_order_id = I('get.return_order_id');
        $row = $rg->alias('rg')->field('return_status,rg.order_id,order,wl_order,goods_id')->join('__RETURN_ITEM__ item ON item.order_id = rg.order_id')->where(array('rg.order_id' => $return_order_id))->find();
        $row2 = $this->company_address($row['goods_id']);
        $list = M('return_goods')->alias('rg')->field('name,price,picture,shop_price,size,color,item.num')
            ->join('__RETURN_ITEM__ item ON item.order_id = rg.order_id')
            ->join('__GOODS__ g ON item.goods_id = g.id')
            ->join('__ORDER_ITEM__ o ON rg.order = o.order_id')
            ->join('__CART__ c ON o.cart_id = c.id')
            ->where(array('item.order_id' => $return_order_id, 'o.order_id' => $row['order']))->group('c.id')->select();
        //调用物流借口
        $exp = A('Express');
        $result  = $exp -> getorder( $row['wl_order']);
        $data = array_merge($row, $row2);

        $this->assign('data', $data);
        $this->assign('result', $result[0]);
        $this->assign('list', $list);
        $this->display();
    }

    public function queryReturnWuliu(){
        $rg = M('return_goods');
        $return_order_id = I('get.return_order_id');
        $row = $rg->field('wl_order,wl_name')->where(array('order_id' => $return_order_id))->find();
        //调用物流借口
        $exp = A('Express');
        $result  = $exp -> getorder( $row['wl_order']);
        $this->assign('row', $row);
        $this->assign('list', $result);
        $this->display();
    }
}